﻿namespace PrismEx.SampleApp.Modules.SplashScreen
{
    using System;
    using System.ComponentModel.Composition;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Interop;

    [Export]
    public partial class Splash : Window
    {
        private readonly ParentDetails parentDetails;

        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int height, bool repaint);



        public Splash(ParentDetails parentDetails = null)
        {
            this.parentDetails = parentDetails;
            this.InitializeComponent();

            this.Loaded += new RoutedEventHandler(Splash_Loaded);
        }

        void Splash_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.parentDetails == null) return;
            var interopHelper = new WindowInteropHelper(this);

            MoveWindow(
                interopHelper.Handle,
                0,
                0,
                parentDetails.Width,
                parentDetails.Height,
                true);

            SetParent(interopHelper.Handle, parentDetails.Handle);
        }
    }

    
}
